package com.ruoyi.web.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.LeaveWordMapper;
import com.ruoyi.web.domain.LeaveWord;
import com.ruoyi.web.service.ILeaveWordService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 留言Service业务层处理
 * 
 * @author LFY
 * @date 2024-02-28
 */
@Service
public class LeaveWordServiceImpl implements ILeaveWordService 
{
    @Resource
    private LeaveWordMapper leaveWordMapper;

    /**
     * 查询留言
     * 
     * @param id 留言主键
     * @return 留言
     */
    @Override
    public LeaveWord selectLeaveWordById(Long id)
    {
        return leaveWordMapper.selectLeaveWordById(id);
    }

    /**
     * 查询留言列表
     * 
     * @param leaveWord 留言
     * @return 留言
     */
    @Override
    public List<LeaveWord> selectLeaveWordList(LeaveWord leaveWord)
    {
        return leaveWordMapper.selectLeaveWordList(leaveWord);
    }

    /**
     * 新增留言
     * 
     * @param leaveWord 留言
     * @return 结果
     */
    @Override
    public AjaxResult insertLeaveWord(LeaveWord leaveWord)
    {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            LeaveWord newlea = new LeaveWord();
            newlea.setTel(leaveWord.getTel());
            newlea.setStatus("1");
            List<LeaveWord> leaveWords = leaveWordMapper.selectLeaveWordList(newlea);
            if (leaveWords.size() == 0) {
                leaveWord.setCreateTime(DateUtils.getNowDate());
                int i = leaveWordMapper.insertLeaveWord(leaveWord);
                if (i == 1){
                    ajaxResult.put("code",200);
                    ajaxResult.put("msg","Message successful, customer service will contact you later!");
                }else{
                    ajaxResult.put("code",500);
                    ajaxResult.put("msg","Message failed, please contact the administrator!");
                }
            }else{
                ajaxResult.put("code",-1);
                ajaxResult.put("msg","Your message information is already available, please do not resubmit!");
            }
        }catch (Exception e){
            ajaxResult.put("code",500);
            ajaxResult.put("msg","Message failed, please contact the administrator!");
            e.printStackTrace();
        }
        return ajaxResult;
    }

    /**
     * 修改留言
     * 
     * @param leaveWord 留言
     * @return 结果
     */
    @Override
    public int updateLeaveWord(LeaveWord leaveWord)
    {
        leaveWord.setUpdateTime(DateUtils.getNowDate());
        leaveWord.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        return leaveWordMapper.updateLeaveWord(leaveWord);
    }

    /**
     * 批量删除留言
     * 
     * @param ids 需要删除的留言主键
     * @return 结果
     */
    @Override
    public int deleteLeaveWordByIds(String ids)
    {
        return leaveWordMapper.deleteLeaveWordByIds(Convert.toStrArray(ids),ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 删除留言信息
     * 
     * @param id 留言主键
     * @return 结果
     */
    @Override
    public int deleteLeaveWordById(Long id)
    {
        return leaveWordMapper.deleteLeaveWordById(id,ShiroUtils.getSysUser().getLoginName());
    }
}
